Arabization = LibStub("AceAddon-3.0"):NewAddon("Arabization", "AceConsole-3.0")

Arabization.FONT = "Interface\\AddOns\\Arabization\\font\\Arabization.ttf";
Arabization.DEBUG = false -- false when release

Arabization.property = {}

function Arabization:OnInitialize()
    self.version = GetAddOnMetadata("Arabization", "Version")
    print(string.format("Welcome to Arabization Ver: %s.\nSetting is /Ar or /Arabization.", self.version))

    self.db = LibStub('AceDB-3.0'):New("ArabizationDB")
	self.db:RegisterDefaults({
        profile = {
            quest       = { questlog = true, worldmap = true, gossip = true, questlog_movable = false },
            item        = { tooltip = true },
            spell       = { tooltip = true, buff = true },
            achievement = { tooltip = true, advice = true },
            developer   = false, -- false when release
            development = { debugger = false }, -- false when release
        }
    })

    -- Quest --
    ArabizationQuestLog:OnInitialize()
    ArabizationWorldMapQuest:OnInitialize()
    ArabizationQuestGossip:OnInitialize()

    -- ToolTip --
    ArabizationGameToolTip:OnInitialize()
    ArabizationItemRefTooltip:OnInitialize()

    ArabizationBuffToolTip:OnInitialize()
    ArabizationGlyphToolTip:OnInitialize()
end

function Arabization:OnEnable()
    self:DebugLog("Arabization: OnEnable.");

    -- Option --
    self:SetupOptions()

    -- Quest --
    ArabizationQuestLog:OnEnable()
    ArabizationWorldMapQuest:OnEnable()
    ArabizationQuestGossip:OnEnable()

    -- ToolTip --
    ArabizationGameToolTip:OnEnable()
    ArabizationItemRefTooltip:OnEnable()

    ArabizationBuffToolTip:OnEnable()
    ArabizationGlyphToolTip:OnEnable()
end

function Arabization:OnDisable()
    self:DebugLog("Arabization: OnDisable.");

    -- Quest --
    ArabizationQuestLog:OnDisable()
    ArabizationWorldMapQuest:OnDisable()
    ArabizationQuestGossip:OnDisable()

    -- ToolTip --
    ArabizationGameToolTip:OnDisable()
    ArabizationItemRefTooltip:OnDisable()

    ArabizationBuffToolTip:OnDisable()
    ArabizationGlyphToolTip:OnDisable()
end

function Arabization:LoadAddOn(addon)
    return true
end

function Arabization:uc(str)
    return string.gsub(str, "(%w)", function(s)
        return string.upper(s)
    end, 1)
end

function Arabization:lc(str)
    return string.gsub(str, "(%w)", function(s)
        return string.lower(s)
    end, 1)
end

function Arabization:SetupOptions()
    function GetOptions()
        return {
            type = "group",
            name = "Arabization",
            args = {
                general = {
                    type = "group",
                    name = "Arabization",
                    args = {
                        desc = {
                            type = "description",
                            name = "Arabization is addon that displays the Arabic translation of Quest information and Spell/Item ToolTips.",
                            order = 1,
                        },
                    },
                },
            },
        }
    end

    LibStub("AceConfig-3.0"):RegisterOptionsTable("Arabization", GetOptions())

    local dialog =  LibStub("AceConfigDialog-3.0")
    dialog:AddToBlizOptions("Arabization", nil, nil, "general")
    dialog:AddToBlizOptions("Arabization", "Quest", "Arabization", "quest")
    dialog:AddToBlizOptions("Arabization", "ToolTip", "Arabization", "tooltip")
    dialog:AddToBlizOptions("Arabization", "Developer", "Arabization", "developer")

    self:RegisterChatCommand("Ar", "ChatCommand")
    self:RegisterChatCommand("Arabization", "ChatCommand")
end

function Arabization:ChatCommand(input)
    if input == 'debug' then
        -- Debug Window --
        if self.db.profile.development.debugger then
            ArabizationDebugFrame:Show()
        end
    else
        self:OpenConfigDialog()
    end
end

function Arabization:OpenConfigDialog()
    LibStub("AceConfigDialog-3.0"):Open("Arabization")
end

function Arabization:DebugLog(args)
    if not self.DEBUG then
        return
    end

    if type(args) == 'string' then
        self:Print(args)
        return
    end

    if type(args) == 'number' then
        self:Print(tostring(args))
        return
    end

    if type(args) == 'table' then
        self:Print("----------")
        for n, v in pairs(args) do
            self:Print(n .. ' : ' .. v)
        end
        self:Print("----------")
        return
    end
end
